using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace MyPhotoIndex.Controls
{
    public class CustomListBox : ListBox
    {
        private ImageList m_imageList;

        public ImageList ImageList
        {
            get { return m_imageList; }
            set { m_imageList = value; }
        }

        public CustomListBox()
        {
            base.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.DrawMode = DrawMode.OwnerDrawFixed;
            base.ItemHeight = 30;
        }

        protected override void OnSelectedIndexChanged(EventArgs e)
        {
            base.Invalidate();
      //      base.OnSelectedIndexChanged(e);
        }

        protected override void OnDrawItem(DrawItemEventArgs e)
        {
            if (base.Items.Count == 0)
            {
                return;
            }

            Graphics g = e.Graphics;
            g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;

            g.FillRectangle(Brushes.White, e.Bounds.X, e.Bounds.Y, e.Bounds.Right, e.Bounds.Height);

            using (Brush brush = new SolidBrush((Color.FromArgb(249, 248, 245))))
            {
                g.FillRectangle(brush, e.Bounds.X, e.Bounds.Y, e.Bounds.X + 29, e.Bounds.Height);
            }

            bool selectd = ((e.State & DrawItemState.Selected) == DrawItemState.Selected);
            if(selectd == true)
            {
                g.DrawImageUnscaled(Properties.Resources.check, e.Bounds.X + 3, e.Bounds.Y + 3);
            }

            using (Pen pen = new Pen(Color.FromArgb(212, 208, 200)))
            {
                g.DrawLine(pen, 30, e.Bounds.Y, 30, e.Bounds.Bottom);
                g.DrawLine(pen, 23, e.Bounds.Bottom - 1, 37, e.Bounds.Bottom - 1);
            }

            if (m_imageList != null &&
                m_imageList.Images.Count >= e.Index)
            {
                Rectangle imageRect = new Rectangle(37, e.Bounds.Y + 7, 16, 16);
                g.DrawImage(m_imageList.Images[e.Index], imageRect);
            }

            g.DrawString(base.Items[e.Index].ToString(), base.Font, Brushes.Black, new PointF(60, e.Bounds.Y + 7));


            //base.OnDrawItem(e);
        }
    }
}
